package com.letwal.cmcvnd.invite

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.anetwork.ApiUrl
import com.letwal.cmcvnd.aviewmodel.UserViewModel
import com.letwal.cmcvnd.base.BaseActivity
import com.letwal.cmcvnd.data.ReqBase
import com.letwal.cmcvnd.databinding.ActivityInviteBinding
import com.letwal.cmcvnd.enums.PageTagEnum
import com.letwal.cmcvnd.extend.copyToClipboard
import com.letwal.cmcvnd.extend.openSysShareTextIntent
import com.letwal.cmcvnd.extend.toFinancePercentString

@SuppressLint("CustomSplashScreen")
class InviteActivity : BaseActivity() {

    private lateinit var mActivityBinding: ActivityInviteBinding

    //user viewModel
    private val mUserViewModel by lazy {
        ViewModelProvider(this)[UserViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mUserViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.home.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding = ActivityInviteBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    override fun onContentViewBinded() {
        try {
            //初始化刷新组件
            initRefreshUI()

            //监听返回
            onHandleLiveDataCallback()

            //点击复制
            mActivityBinding.submitInviteLinkCopy.setOnClickListener {
                copyToClipboard(mActivityBinding.textInviteLinkValue.text.toString().trim())
            }

            //点击分享
            mActivityBinding.submitInviteShare.setOnClickListener {
                val body = getString(R.string.invite_share_content)
                val link = mActivityBinding.textInviteLinkValue.text.toString().trim()
                val content = body + "\n\n" + link
                openSysShareTextIntent(content)
            }

            //获取分享信息
            mUserViewModel.getMyInviteInfo(ReqBase(showLoading = false))

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /** 监听live data 返回 */
    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    private fun onHandleLiveDataCallback() {
        // 获取邀请码返回
        mUserViewModel.onGetMyInviteInfoLiveData.observe(this) {
            stopRefreshState()
            if (it.isSuccess) {
                it.getOrNull()?.let { result ->
                    val refer = result.inviteCode
                    val rate = result.incomeRate.toFinancePercentString()

                    // 分享链接
                    mActivityBinding.textInviteRateValue.text = rate
                    mActivityBinding.textInviteLinkValue.text = "${ApiUrl.BASE_URL}?referrer=$refer"
                }
            }
        }
    }

    /** 初始化下拉刷新 */
    private fun initRefreshUI() {
        connectSwipeRefreshLayout(mActivityBinding.swipeRefresh) {
            //获取分享信息
            mUserViewModel.getMyInviteInfo(ReqBase(showLoading = false))
        }
    }
}